import axios from 'axios'
import qs from 'qs'
import utils from '../common/utils'
import es6promise from 'es6-promise'
es6promise.polyfill();



class baseModel {

    constructor(options) {
        Object.assign(this,options);
        this.axios = axios;
        // this.axios.defaults.headers.post['Content-Type'] = 'application/json';
    }


    $get(url,params) {
        return new Promise ((resolve,reject) =>{
            axios.get(url,{
                params
            }).then(response => {
                return resolve(response.data);
            }).catch(err => {
                return reject(err);
            })
        })
    }

    $post(url,data) {
        return new Promise ((resolve,reject) => {
            // console.log(data);
            axios.post(url,data).then(response => {
                return resolve(response.data);
            }).catch(err => {
                return reject(err);
            })
        })
    }

    $put(url,data) {
        return new Promise ((resolve,reject) => {
            axios.post(url,qs.stringify(data)).then(response => {
                return resolve(response.data);
            }).catch(err => {
                return reject(err);
            })
        })
    }

    $delete (url,params = {}){
        return new Promise ((resolve,reject) => {
            axios.delete(url,qs.stringigy({params})).then(response => {
                return resolve(response.data);
            }).catch(err => {
                return reject(err);
            })
        })
    }
    
    $patch (url,data){
        return new Promise ((resolve,reject) => {
            axios.patch(url,qs.stringify(data)).then(response => {
                return resolve(response.data);
            }).catch(err => {
                return reject(err);
            })
        })
    }
}
export default baseModel;